/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.basics.editor.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.net.URL;
import java.rmi.server.UID;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import com.genia.toolbox.basics.editor.AbstractController;
import com.genia.toolbox.basics.editor.message.AbstractMessages;
import com.genia.toolbox.basics.editor.model.bean.InitialSettings;
import com.genia.toolbox.basics.editor.model.document.Document;
import com.genia.toolbox.basics.editor.model.document.Element;

/**
 * Define an abstract menu bar.
 * 
 * @param <G>
 *          The generic GUI
 * @param <C>
 *          The generic controller.
 * @param <D>
 *          The generic document
 * @param <E>
 *          The generic element.
 * @param <I>
 *          The generic initial settings
 */
@SuppressWarnings("serial")
public abstract class AbstractMenuBar<G extends AbstractGUI<?, ?, C, D, E, I>, C extends AbstractController<?, ?, D, E, I>, D extends Document<E>, E extends Element<?>, I extends InitialSettings>
    extends JMenuBar
    implements ActionListener
{

  /**
   * The gui.
   */
  private G gui;

  /**
   * The create template item.
   */
  private JMenuItem itemCreateTemplate = null;

  /**
   * The open template item.
   */
  private JMenuItem itemOpenTemplate = null;

  /**
   * The save template item.
   */
  private JMenuItem itemSaveTemplate = null;

  /**
   * The save all template item.
   */
  private JMenuItem itemSaveAllTemplates = null;

  /**
   * The cut template item.
   */
  private JMenuItem itemCutTemplate = null;

  /**
   * The paste template item.
   */
  private JMenuItem itemPasteTemplate = null;

  /**
   * The close template item.
   */
  private JMenuItem itemCloseTemplate = null;

  /**
   * The close all template item.
   */
  private JMenuItem itemCloseAllTemplates = null;

  /**
   * The close item.
   */
  private JMenuItem itemClose = null;



  /**
   * Constructor.
   * 
   * @param gui
   *          The gui.
   */
  public AbstractMenuBar(G gui)
  {
    super();
    this.gui = gui;
  }



  /**
   * Initialize the common commands of the menu bar.
   */
  public void initialise()
  {
    JMenu menuFile = new JMenu(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_MENUBAR_FILE_TITLE));

    this.itemCreateTemplate = new JMenuItem(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_ACTION_CREATE));
    this.itemCreateTemplate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
    URL url = AbstractMenuBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/create.gif");
    this.itemCreateTemplate.setIcon(new ImageIcon(url));
    this.itemCreateTemplate.addActionListener(this);
    menuFile.add(this.itemCreateTemplate);

    this.itemOpenTemplate = new JMenuItem(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_ACTION_OPEN));
    this.itemOpenTemplate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
    url = AbstractMenuBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/open.gif");
    this.itemOpenTemplate.setIcon(new ImageIcon(url));
    this.itemOpenTemplate.addActionListener(this);
    menuFile.add(this.itemOpenTemplate);

    menuFile.addSeparator();

    this.itemCutTemplate = new JMenuItem(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_ACTION_CUT));
    this.itemCutTemplate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
    url = AbstractMenuBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/cut.gif");
    this.itemCutTemplate.setIcon(new ImageIcon(url));
    this.itemCutTemplate.addActionListener(this);
    menuFile.add(this.itemCutTemplate);

    this.itemPasteTemplate = new JMenuItem(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_ACTION_PASTE));
    this.itemPasteTemplate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
    url = AbstractMenuBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/paste.gif");
    this.itemPasteTemplate.setIcon(new ImageIcon(url));
    this.itemPasteTemplate.addActionListener(this);
    menuFile.add(this.itemPasteTemplate);

    menuFile.addSeparator();

    this.itemSaveTemplate = new JMenuItem(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_ACTION_SAVE));
    this.itemSaveTemplate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
    url = AbstractMenuBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/save.gif");
    this.itemSaveTemplate.setIcon(new ImageIcon(url));
    this.itemSaveTemplate.addActionListener(this);
    menuFile.add(this.itemSaveTemplate);

    this.itemSaveAllTemplates = new JMenuItem(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_ACTION_SAVEALL));
    this.itemSaveAllTemplates.addActionListener(this);
    menuFile.add(this.itemSaveAllTemplates);

    menuFile.addSeparator();

    this.itemCloseTemplate = new JMenuItem(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_ACTION_CLOSE));
    this.itemCloseTemplate.addActionListener(this);
    menuFile.add(this.itemCloseTemplate);

    this.itemCloseAllTemplates = new JMenuItem(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_ACTION_CLOSEALL));
    this.itemCloseAllTemplates.addActionListener(this);
    menuFile.add(this.itemCloseAllTemplates);

    menuFile.addSeparator();

    this.itemClose = new JMenuItem(this.getGui().getController().notifyTranslation(AbstractMessages.BASIC_ACTION_QUIT));
    this.itemClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));
    this.itemClose.addActionListener(this);
    menuFile.add(this.itemClose);

    this.add(menuFile);

    this.initialiseSpecificCommands();
  }



  /**
   * Initialise the specific commands of the menu bar.
   */
  public abstract void initialiseSpecificCommands();



  /**
   * Refresh the access of the items.
   */
  public void refresh()
  {
    this.itemCreateTemplate.setEnabled(true);
    this.itemOpenTemplate.setEnabled(true);
    Document<E> template = this.getGui().getSelectedDocument();
    if (template == null) {
      this.itemSaveTemplate.setEnabled(false);
      this.itemCloseTemplate.setEnabled(false);
    }
    else {
      Element<?> element = this.getGui().getSelectedElement();
      if (element != null && element.getParentID() != null) {
        this.itemCutTemplate.setEnabled(true);
      }
      else {
        this.itemCutTemplate.setEnabled(false);
      }
      if (this.getGui().getController().notifyHasCutElement()) {
        if (element != null && element.isContainer()) {
          this.itemPasteTemplate.setEnabled(true);
        }
        else {
          this.itemPasteTemplate.setEnabled(false);
        }
      }
      else {
        this.itemPasteTemplate.setEnabled(false);
      }
    }
    List<UID> templates = this.getGui().getAllDocumentsUniqueID();
    if (templates.isEmpty()) {
      this.itemSaveAllTemplates.setEnabled(false);
      this.itemCloseAllTemplates.setEnabled(false);
    }
    else {
      this.itemSaveAllTemplates.setEnabled(true);
      this.itemCloseAllTemplates.setEnabled(true);
    }
  }



  /**
   * Handle the action events.
   * 
   * @param e
   *          the action event.
   */
  public void actionPerformed(ActionEvent e)
  {

    Object source = e.getSource();
    if (source.equals(this.itemClose)) {
      this.getGui().getController().notifyCloseApplication();
    }
    else if (source.equals(this.itemOpenTemplate)) {
      this.getGui().getController().notifyCreateOpenDocument();
    }
    else if (source.equals(this.itemCloseTemplate)) {
      D document = this.getGui().getSelectedDocument();
      this.getGui().getController().notifyCloseDocument(document);
    }
    else if (source.equals(this.itemCloseAllTemplates)) {
      this.getGui().getController().notifyCloseAllDocuments();
    }
    else if (source.equals(this.itemCreateTemplate)) {
      I settings = this.getGui().createElement();
      this.getGui().getController().notifyCreateNewDocument(settings);
    }
    else if (source.equals(this.itemSaveTemplate)) {
      D document = this.getGui().getSelectedDocument();
      this.getGui().getController().notifySaveDocument(document);
    }
    else if (source.equals(this.itemSaveAllTemplates)) {
      this.getGui().getController().notifySaveAllDocuments();
    }
    else if (source.equals(this.itemCutTemplate)) {
      E element = this.getGui().getSelectedElement();
      this.getGui().getController().notifyCutElement(element);
    }
    else if (source.equals(this.itemPasteTemplate)) {
      E element = this.getGui().getSelectedElement();
      this.getGui().getController().notifyPasteElement(element);
    }
  }



  /**
   * Get the gui.
   * 
   * @return the gui.
   */
  public G getGui()
  {
    return this.gui;
  }



  /**
   * Set the gui.
   * 
   * @param gui
   *          the gui.
   */
  public void setGui(G gui)
  {
    this.gui = gui;
  }



  /**
   * Get the cut item.
   * 
   * @return the cut item.
   */
  public JMenuItem getButtonCut()
  {
    return this.itemCutTemplate;
  }



  /**
   * Get the paste item.
   * 
   * @return the paste item.
   */
  public JMenuItem getButtonPaste()
  {
    return this.itemPasteTemplate;
  }

}
